
/**
 *  \file write_task_internal.h
 *
 *  Module Internal Header File for Write Task module.
 */

/*
 *  Copyright (C) 2013. Mindtree Ltd.
 *  All rights reserved.
 */

#ifndef _H_WRITE_TASK_INTERNAL_
#define _H_WRITE_TASK_INTERNAL_

/* ----------------------------------------------- Header File Inclusion */
#include "BT_common.h"
#include "write_task.h"
#include "write_task_pl.h"


/* ----------------------------------------------- Write Task Debug */
#ifndef WT_NO_DEBUG
#define WT_ERR(...)                             BT_debug_error(BT_MODULE_ID_WT, __VA_ARGS__)
#define WT_SPY_LOG(...)                         BT_debug_trace(BT_MODULE_ID_WT, __VA_ARGS__)
#else  /* WT_NO_DEBUG */
#define WT_ERR                                  BT_debug_null
#define WT_SPY_LOG                              BT_debug_null
#endif /* WT_NO_DEBUG */

#ifdef WT_DEBUG

#define WT_TRC(...)                             BT_debug_trace(BT_MODULE_ID_WT, __VA_ARGS__)
#define WT_INF(...)                             BT_debug_info(BT_MODULE_ID_WT, __VA_ARGS__)

#else /* WT_DEBUG */

#define WT_TRC                                  BT_debug_null
#define WT_INF                                  BT_debug_null

#endif /* WT_DEBUG */


/* ----------------------------------------------- Global Definitions */
/** Write Task State Value */
#define WT_INIT                                 0x00
#define WT_WAITING                              0x01
#define WT_SERVICING                            0x02
#define WT_SHUTDOWN                             0x03

/** Number of Write Task Queues - Internal, API & Data Queues */
#define WT_NUM_QUEUES                           3


/* ----------------------------------------------- Structures/Data Types */
/** Write Task Queue Element */
typedef struct
{
    /* Bottom Half Function Pointer */
    void (* bh_func) ( void *, UINT16 );

    /* "Allocated" Data Pointer */
    UCHAR * allocated_data;

    /* User Data Length */
    UINT16 data_length;

    /* Dummy Variable */
    UINT16 dummy;

    /* Static Data Pointer */
    BT_DEFINE_STATIC_DATA(static_data)

} WT_Q_ELEMENT;


/**
 *  Information of Write Task Queue.
 *
 *  Start index of any queue is accessed when the queue element is dequeued
 *  for processing. End index of any queue is accessed when an element is
 *  to be enqueud into a particular queue.
 */
typedef struct
{
    /** Start index of the queue */
    UCHAR q_start;

    /** End index of the queue */
    UCHAR q_end;

    /** Maximum limit of the queue */
    UCHAR q_limit;

#ifdef WT_ENABLE_SPY
    /** Number of elements in the queue */
    UCHAR q_count;
#endif /* WT_ENABLE_SPY */

    /** Pointer to each queue */
    WT_Q_ELEMENT *q_ptr;

} WT_QUEUES;
#ifdef WT_ENABLE_SPY
typedef struct wt_spy
{
    /* Maximum Used Queue Elements */
    UINT32 max_used_q_elem[WT_NUM_QUEUES];

    /**
     * Maximum number of Bottom Half functions present in these Queues.
     * This is a cumulative number accross the Queues.
     */
    UINT32 max_bh_counter;

} WT_SPY;
#endif /* WT_ENABLE_SPY */

/* ----------------------------------------------- Macros */
#ifdef WT_ENABLE_SPY
#define WT_INCREMENT_Q_COUNT(q) \
        q.q_count ++

#define WT_DECREMENT_Q_COUNT(q) \
        q.q_count --

#define WT_SPY_UPDATE_MAX_COUNT(cur_val, new_val) \
        (cur_val) = BT_GET_MAX((cur_val), (new_val))
#else
#define WT_INCREMENT_Q_COUNT(q)
#define WT_DECREMENT_Q_COUNT(q)
#define WT_SPY_UPDATE_MAX_COUNT(cur_val, new_val)
#endif /* WT_ENABLE_SPY */

/* ----------------------------------------------- Function Declarations */
/** The Write Task Start Routine */
BT_THREAD_RETURN_TYPE write_task_start_routine
                      (
                          BT_THREAD_ARGS args
                      );

/** The Write Task Service Routine */
void wt_service_queue
     (
         void
     );

/** Debug routine to print the information of a any Write Task queue. */
#ifdef WT_DEBUG
void wt_queue_debug(UCHAR queue_type);
#endif /* WT_DEBUG */

#ifdef WT_ENABLE_SPY
void wt_spy_init(void);
void wt_spy_bt_init(void);
void wt_spy_log(void);
#else
#define wt_spy_init()
#define wt_spy_bt_init()
#define wt_spy_log()
#endif /* WT_ENABLE_SPY */

#endif /* _H_WRITE_TASK_INTERNAL_ */

